#
# Dispersão despesas 2018 SP
#

library(tidyverse)
library(stringdist)
library(rgdal)
library("maptools")
library(broom)
library(sf)
library(gridExtra)

load("extrSP.RData")
load("cidadesDF.RData")
load("cidades.RData")
load("totVot.RData")

# Verifica a distribuição de movimentações por situação pós-eleitoral (Figura 1)

barplot(prop.table(table(extrUFfim$situacao)), cex.names = 0.6)

# Verifica a média de valores gastos por situação pós-eleitoral (Figura 2)

boxplot(log(extrUFfim$valor) ~ extrUFfim$situacao, cex.axis = 0.7, xlab = "",ylab = "", cex.names = 0.6)

# Verifica quais são e o número de municípios onde cada candidato fez transações

b2 <- tapply(extrUFfim$municipio, extrUFfim$numcand, function(x) table(x))
b3 <- tapply(extrUFfim$municipio, extrUFfim$numcand, function(x) length(unique(x)))
hist(b3, main = "Distribução do número de municípios em que cada candidato realizou depósitos",
     xlab = "Número de Municípios",
     ylab = "Frequência")
b3 <- data.frame("numcand" = names(b3), "numMun" = b3)

tmp <- merge(extrUFfim, b3, by = "numcand", all.x = T)



# Calcula a proporção dos depósitos no município mais ferquente sobre o total
# de cada candidato

pratio <- tapply(tmp$municipio, tmp$numcand, function(x) max(table(x),0)/sum(table(x)+1))
boxplot(pratio[pratio > 0]) # Figura 3

numMunCand <- tapply(tmp$numMun,tmp$situacao, median)
boxplot(tmp$numMun ~ tmp$situacao, cex.axis = 0.7, xlab = "",ylab = "") # Figura 5

## Verifica proporção dos votos totais que vieram dos locais onde fez campanha
## Exemplo de como a base pode ser comparada com outras bases como a de resultados eleitorais


totVot <- data.frame("id" = names(totVot), "votos" = totVot)
agMun <- aggregate(votos ~ municipio + numcand, data = extrUFfim, FUN = mean)
votBase <- tapply(agMun$votos, agMun$numcand, sum)
votBase <- data.frame("id" = names(votBase), "votos" = votBase)

votBase <- votBase %>% right_join(totVot, by = "id")

boxplot(votBase[2]/votBase[3])

# Verifica os partidos que realizaram mais transações

munPart <- tapply(extrUFfim$municipio,extrUFfim$partido, function(x) length(unique(x)))
munPart <- munPart[order(munPart, decreasing = T)]
barplot(munPart, cex.names = 0.5, las = 2)


#################### MAPAS #######################################################################



map <- ggplot() + geom_polygon(data = cidades,
                               aes(x = long, y = lat, group = group),
                               colour = "darkgrey",
                               fill = NA,
                               size = 0.3)

# Figura 4

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(tdesp)),
                   linewidth = 0,
                   colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",
                      low = "#3300CC", high = "#CC00CC",
                      na.value = NA) +
  ggtitle("Todas Candidatas") +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 10))



# Votação dos 6 mais votados

e1 <- map + geom_polygon(data = cidades.df,
                         aes(long,lat,group = group, fill = log10(cidades.df$'1720')),
                         linewidth = 0,
                         colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Eduardo Bolsonaro") +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

e2 <- map + geom_polygon(data = cidades.df,
                         aes(long,lat,group = group, fill = log10(cidades.df$'1771')),
                         linewidth = 0,
                         colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Joice Hasselmann") +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

e3 <- map + geom_polygon(data = cidades.df,
                         aes(long,lat,group = group, fill = log10(cidades.df$'1000')),
                         colour = "gray",
                         linewidth = 0,
                         size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Celso Russomano")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

e4 <- map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'2555')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Kim Kataguiri")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

e5 <- map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'2222')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Francisco Everardo")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

e6 <- map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'1200')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Tabata Amaral")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

grid.arrange(e1,e2,e3) # Figura 6


# Votação dos seis mais votados entre os eleitos por média

em1 <- map + geom_polygon(data = cidades.df,
                          aes(long,lat,group = group, fill = log10(cidades.df$'2533')),
                          linewidth = 0,
                          colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "David Bezerra Soares")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

em2 <- map + geom_polygon(data = cidades.df,
                          aes(long,lat,group = group, fill = log10(cidades.df$'4515')),
                          linewidth = 0,
                          colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Eduardo Cury")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

em3 <- map + geom_polygon(data = cidades.df,
                          aes(long,lat,group = group, fill = log10(cidades.df$'2277')),
                          linewidth = 0,
                          colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Miguel Lombardi")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))


map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'2577')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Adriano Eli Correia")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'2020')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Gilberto Nascimento")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'2550')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Eugenio José  Zuliani")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

grid.arrange(em1,em2,em3)

# Os seis mais votados entre os suplentes

s1 <- map + geom_polygon(data = cidades.df,
                         aes(long,lat,group = group, fill = log10(cidades.df$'4547')),
                         linewidth = 0,
                         colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Miguel Haddad")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

s2 <- map + geom_polygon(data = cidades.df,
                         aes(long,lat,group = group, fill = log10(cidades.df$'1152')),linewidth = 0,
                         colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Arnaldo Faria  de Sá")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

s3 <- map + geom_polygon(data = cidades.df,
                         aes(long,lat,group = group, fill = log10(cidades.df$'2511')),
                         linewidth = 0,
                         colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Missionário José Olimpio")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'5555')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Eleuses Paiva")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'4577')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Carlos Bezerra Jr.")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'4000')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Luiz Lauro Filho")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

grid.arrange(s1,s2,s3)


# Os seis mais votados entre os não-eleitos

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'1818')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Kayo Amado")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'5151')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Adilson Barroso")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'2828')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Levy Fidelix")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'9090')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Ricardo Teixeira")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'1800')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Marcos Papa.")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'1808')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Wellington Nogueira")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

# Número de municípios por partido

barplot(tapply(extrSPr$municipio,extrSPr$partido, function(x) length(unique(x))), cex.names = 0.7, las = 2)

barplot(table(extrSPr$partido)/tapply(extrSPr$numcand,extrSPr$partido, function(x) length(unique(x))), cex.names = 0.7, las = 2)

munPart <- tapply(extrSPr$municipio, extrSPr$partido, function(x) length(unique(x)))

# PMDB

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'1515')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Baleia  Rossi")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'1551')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Herculano Passos Jr.")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'1500')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Paulo Mansur")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'1555')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Junji Abe")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

#map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log(cidades.df$'1588')),colour = "black", size = 0.3) +
#  coord_equal() +
#  scale_fill_gradient(low = "#3300CC", high = "#CC00CC", na.value = NA) +
#  labs(title = "Vitor Rodrigues")

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'1507')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Antonio Matias dos Santos")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))


# PR

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'2222')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Tiririca")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

map + geom_polygon(data = cidades.df,  aes(long,lat,group = group, fill = log10(cidades.df$'2240')),colour = "black", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Policial Katia  Sastre")  +
  theme(legend.position = "bottom",
        legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5))

pr1 <-  map + geom_polygon(data = cidades.df,
                           aes(long,lat,group = group, fill = log10(cidades.df$'2200')),
                           linewidth = 0,
                           colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Capitão Augusto")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

pr2 <-  map + geom_polygon(data = cidades.df,
                           aes(long,lat,group = group, fill = log10(cidades.df$'2299')),
                           linewidth = 0,
                           colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Marcio Alvino")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

pr3 <-  map + geom_polygon(data = cidades.df,
                           aes(long,lat,group = group, fill = log10(cidades.df$'2233')),
                           linewidth = 0,
                           colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC", na.value = NA) +
  labs(title = "Paulo Freire Costa")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

pr4 <-  map + geom_polygon(data = cidades.df,
                           aes(long,lat,group = group, fill = log10(cidades.df$'2277')),
                           linewidth = 0,
                           colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Miguel Lombardi")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

pr5 <-  map + geom_polygon(data = cidades.df,
                           aes(long,lat,group = group, fill = log10(cidades.df$'2244')),
                           linewidth = 0,
                           colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Luiz Carlos Motta")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

pr6 <-  map + geom_polygon(data = cidades.df,
                           aes(long,lat,group = group, fill = log10(cidades.df$'2255')),
                           linewidth = 0,
                           colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Milton Monti")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

grid.arrange(pr1, pr2, pr3, pr4, pr5, pr6, nrow = 3)


# PSDB

psdb1 <-  map + geom_polygon(data = cidades.df,
                             aes(long,lat,group = group, fill = log10(cidades.df$'4585')),
                             linewidth = 0,
                             colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Bruna Furlan")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

psdb2 <-  map + geom_polygon(data = cidades.df,
                             aes(long,lat,group = group, fill = log10(cidades.df$'4500')),
                             linewidth = 0,
                             colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Carlos Sampaio")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

psdb3 <-  map + geom_polygon(data = cidades.df,
                             aes(long,lat,group = group, fill = log10(cidades.df$'4510')),
                             linewidth = 0,
                             colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Vitor Lipi")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

psdb4 <-  map + geom_polygon(data = cidades.df,
                             aes(long,lat,group = group, fill = log10(cidades.df$'4580')),
                             linewidth = 0,
                             colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Samuel Moreira")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

psdb5 <-  map + geom_polygon(data = cidades.df,
                             aes(long,lat,group = group, fill = log10(cidades.df$'4551')),
                             linewidth = 0,
                             colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC", na.value = NA) +
  labs(title = "Vanderlei Macris")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

psdb6 <-  map + geom_polygon(data = cidades.df,
                             aes(long,lat,group = group, fill = log10(cidades.df$'4515')),
                             linewidth = 0,
                             colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Eduardo Cury")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))



grid.arrange(psdb1,psdb2,psdb3,psdb4,psdb5,psdb6, nrow = 3)


# PT

pt1 <-  map + geom_polygon(data = cidades.df,
                           aes(long,lat,group = group, fill = log10(cidades.df$'1313')),
                           linewidth = 0,
                           colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Rui Falcão")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

pt2 <-  map + geom_polygon(data = cidades.df,
                           aes(long,lat,group = group, fill = log10(cidades.df$'1370')),
                           linewidth = 0,
                           colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Carlos Zaratini")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

pt3 <-  map + geom_polygon(data = cidades.df,
                           aes(long,lat,group = group, fill = log10(cidades.df$'1353')),
                           linewidth = 0,
                           colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Nilton Tatto")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

pt4 <-  map + geom_polygon(data = cidades.df,
                           aes(long,lat,group = group, fill = log10(cidades.df$'1354')),
                           linewidth = 0,
                           colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Alexandre Padilha")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

pt5 <-  map + geom_polygon(data = cidades.df,
                           aes(long,lat,group = group, fill = log10(cidades.df$'1322')),
                           linewidth = 0,
                           colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC", na.value = NA) +
  labs(title = "Arlindo Chinaglia")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))

pt6 <-  map + geom_polygon(data = cidades.df,
                           aes(long,lat,group = group, fill = log10(cidades.df$'1398')),
                           linewidth = 0,
                           colour = "gray", size = 0.3) +
  coord_equal() +
  scale_fill_gradient(name = "Volume (log10)",low = "#3300CC", high = "#CC00CC",na.value = NA) +
  labs(title = "Paulo Teixeira")  +
  theme(legend.title = element_text(size=8,vjust = 0.9),
        legend.text = element_text(size=8, hjust = 1),
        legend.key.size = unit(0.4, 'cm'),
        rect = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 8))



grid.arrange(pt1,pt2,pt3,pt4,pt5,pt6, nrow = 3)
